package demo2;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-08-23
 * Time: 15:43
 */
public class BinaryTree {
    public  static class TreeNode{
        public  char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    @Override
    public String toString() {
        return "BinaryTree{}";
    }

    public TreeNode createTree(){
        TreeNode A =new TreeNode('A');
        TreeNode B =new TreeNode('B');
        TreeNode C =new TreeNode('C');
        TreeNode D =new TreeNode('D');
        TreeNode E =new TreeNode('E');
        TreeNode F =new TreeNode('F');
        TreeNode G =new TreeNode('G');
        TreeNode H =new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");//先打印了出来，才去调递归函数
        preOrder(root.left);
        preOrder(root.right);
    }
    //带有返回值的前序遍历
    public List<Character> preorderTraversal(TreeNode root){
        List<Character> ret=new ArrayList<>();
        if(root==null){
            return ret;
        }
        ret.add(root.val);//先把数据放到了顺序表中，再去调的回调函数
        List<Character> leftTree=preorderTraversal(root.left);
        ret.addAll(leftTree);
        List<Character> rightTree=preorderTraversal(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    //中序遍历
    public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    //获取树中节点的个数
    public static  int usedSize=0;
    public int size(TreeNode root){
        if(root==null){
            return 0;
        }
        usedSize++;
        size(root.left);
        size(root.right);
        return usedSize;
    }
    //另一种方法
    public  int size2(TreeNode root){
        if(root==null){
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }

    //获取叶子节点的个数
    public static int leafSize=0;
    public int getLeafNodeCount(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }
    //另一种方法
    public int getLeafNodeCount2(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)
                +getKLevelNodeCount(root,k-1);
    }

    // 获取二叉树的高度
    public  int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftH=getHeight(root.left);
        int rightH=getHeight(root.right);
        return (leftH>rightH?leftH:rightH)+1;
    }

    // 检测值为value的元素是否存在(前序遍历)
    public TreeNode find(TreeNode root,int val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode leftL=find(root.left,val);
        if(leftL!=null){
            return leftL;
        }
        TreeNode rightR=find(root.right,val);
        if(rightR!=null){
            return rightR;
        }
        return null;
    }

    //层序遍历
    public  void levelOrder(TreeNode root){
        Queue<TreeNode> queue =new LinkedList<>();//以双向链表的形式建立一个队列
        if(root!=null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            TreeNode top=queue.poll();
            System.out.print(top.val+" ");
            if(top.left!=null){
                queue.offer(top.left);
            }
            if(top.right!=null){
                queue.offer(top.right);
            }
        }
    }

    // 判断一棵树是不是完全二叉树
    public boolean isComplete(TreeNode root){
        Queue<TreeNode> queue =new LinkedList<>();
        if(root!=null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                return false;
            }
        }
        return true;
    }

    //获取路径
    public boolean getpath(TreeNode root, TreeNode node, Stack<TreeNode> stack){
        if(root==null){
            return false;
        }
        stack.push(root);
        if(root==node){
            return true;
        }
        boolean ret=getpath(root.left,node,stack);
        if(ret==true){
            return true;
        }
        boolean ret2=getpath(root.right,node,stack);
        if(ret2==true){
            return true;
        }
        stack.pop();
        return false;
    }

    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor2(TreeNode root,TreeNode p,TreeNode q){
        if(root==null){
            return null;
        }
        Stack<TreeNode> s1=new Stack<>();
        getpath(root,p,s1);
        Stack<TreeNode> s2=new Stack<>();
        getpath(root,q,s2);
        int size1=s1.size();
        int size2=s2.size();
        if(size1>size2){
            int size=size1-size2;
            while(size!=0){
                s1.pop();
                size--;
            }
        }else{
            int size=size2-size1;
            while(size!=0){
                s2.pop();
                size--;
        }
            while(!s1.empty()){
                TreeNode temp1=s1.pop();
                TreeNode temp2=s2.pop();
                if(temp1==temp2){
                    return temp2;
                }
            }
        }
        return null;
    }
}
